<template>
  <div>
    <div class="filter-container mt-15">
      <el-date-picker v-model="listQuery.datetime[0]" class="filter-item" type="date" placeholder="开始日期"></el-date-picker>
      <el-date-picker v-model="listQuery.datetime[1]" class="filter-item" type="date" placeholder="结束日期"></el-date-picker>
      <el-select v-model="listQuery.cpid" class="filter-item" clearable placeholder="渠道包ID">
        <el-option
          v-for="item in cpList"
          :key="item.cpid"
          :label="item.cpname"
          :value="item.cpid"
        />
      </el-select>
      <el-select v-model="listQuery.sid" class="filter-item" style="width: 130px;" filterable clearable placeholder="选择区服">
        <el-option v-for="item in serverList" :key="item.sid" :label="item.name+'('+item.sid+')'" :value="item.sid"> </el-option>
      </el-select>
      <el-input v-model="listQuery.tag" class="filter-item" style="width: 100px;" placeholder="类型ID" />
      <el-input v-model="listQuery.actor" class="filter-item" style="width: 200px;" placeholder="角色ID/角色名/账号" />
      <el-input v-model="listQuery.device" class="filter-item" style="width: 200px;" placeholder="设备码" />
      <el-button class="filter-item" type="primary" plain icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button :loading="downloadLoading" type="primary" icon="el-icon-download" class="filter-item" @click="handleDownload()">导出</el-button>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" border stripe style="width: 100%;" height="685">
      <el-table-column label="渠道包ID" prop="cpid" align="center" width="70"></el-table-column>
      <el-table-column label="服务器ID" prop="sid" align="center" width="90"></el-table-column>
      <el-table-column label="日志ID" prop="tag" align="center" width="75"></el-table-column>
      <el-table-column label="日志描述" prop="name" align="center" width="180"></el-table-column>
      <el-table-column label="信息" show-overflow-tooltip="true" prop="msg" align="center"></el-table-column>
      <el-table-column label="账号" prop="account" align="center" width="150"></el-table-column>
      <el-table-column label="角色ID" prop="actor_id" align="center" width="120"></el-table-column>
      <el-table-column label="角色名" prop="actor_name" align="center" width="150"></el-table-column>
      <el-table-column label="新玩家" align="center" width="50">
        <template slot-scope="{row}">
          <span v-if="row.is_new === 1" style="color: green;">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column label="IP" prop="ip" align="center" width="120"></el-table-column>
      <el-table-column label="设备码" prop="device" align="center" width="130"></el-table-column>
      <el-table-column label="版本" prop="version" align="center" width="80"></el-table-column>
      <el-table-column label="时间" prop="load_dt" align="center" width="150"></el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { getLoadLogList } from '@/api/actorInfo'
import { getServerList, getChannelList, getChannelPackageList } from '@/api/ops'

export default {
  name: 'LoadLog',
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: false,
      downloadLoading: false,
      serverList: [],
      channelList: [],
      listQuery: {
        page: 1,
        limit: 200,
        tag: '',
        device: '',
        actor: '',
        sid: '',
        cpid: '',
        datetime: [new Date(), new Date()]
      }
    }
  },
  created() {
    this.getList()
    this.getServerList()
    // this.getChannelList()
    this.getCpList()
  },
  methods: {
    async getCpList() {
      const res = await getChannelPackageList()
      this.cpList = res.data.items
    },
    async getChannelList() {
      const res = await getChannelList()
      this.channelList = res.data.items
    },
    async getServerList(params) {
      const query = params
      const res = await getServerList(query)
      this.serverList = res.data.items
    },
    async getList() {
      const query = this.listQuery
      this.listLoading = true
      const res = await getLoadLogList(query)
      this.listLoading = false
      this.list = res.data.items
      this.total = res.data.total
      this.$nextTick(() => {
        this.$refs.table.doLayout()
      })
    },
    handleFilter() {
      this.getList()
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['渠道包ID', '服务器ID', '日志ID', '日志描述', '账号', '角色ID', '角色名', '新玩家', 'IP', '设备码', '版本', '时间']
        const filterVal = ['cpid', 'sid', 'tag', 'name', 'account', 'actor_id', 'actor_name', 'is_new', 'ip', 'device', 'version', 'load_dt']
        // const data = this.formatJson(filterVal)
        getLoadLogList(Object.assign({}, this.listQuery, { export: 1 })).then(response => {
          this.dlist = response.data.items
          const data = this.dlist.map(v => filterVal.map(j => {
            return v[j]
          }))
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: '打点日志'
          })
          this.downloadLoading = false
        })
      })
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        return v[j]
      }))
    }
  }
}
</script>
